<?php
include_once("include/session.php");
include_once("geocoder.php");


if(isset($_POST['subeditorg']) && ($_POST['button'] == "Submit Edit")){
	wikiEditInfo();
}
else if(isset($_POST['subeditorg']) && ($_POST['button'] == "Mark Organization Inactive")){
	wikiMarkOrganizationInactive();
}else if(isset($_POST['subeditorg']) && ($_POST['button'] == "Cancel Edit")){
	header("Location: ./food_map.php");
}


function wikiMarkOrganizationInactive(){
	global $database, $session;
	
	$query = "INSERT INTO ".TBL_UPDATE_QUEUE." (timestamp, orgID, UpdatersComments , BusinessIsInactive ) VALUES ("
	.time().", ".$session->clean_Data($_POST["orgID"]).", '".$session->clean_Data($_POST["comments"])."', true)";

	$stmt =  $database->connection->prepare($query);
	$stmt->execute();


	echo "Organization ".$_POST['orgName']." Marked Inactive and Queued for Apporoval ";

}

function wikiEditInfo(){

	global $session, $form;
	/* Org edit attempt */
	$address = $_POST['streetAddress']." ".$_POST['streetAddress2']." ".$_POST['state']." ".$_POST['zipCode'];
	$geocodeAddress = preg_replace('/\s+/', '+', $address);
	$geocode = geocode($geocodeAddress);
	$lat = $geocode["lat"];
	$lng = $geocode["lng"];
	
	$retval = $session->registerOrg(TBL_UPDATE_QUEUE,$_POST['orgName'], $_POST['websiteAddress'], 
										$_POST['category'], $_POST['orgEmail'], $_POST['orgPhoneNumber'],
										$_POST['orgFaxNumber'], $_POST['streetAddress'], 
										$_POST['streetAddress2'], $_POST['city'], $_POST['state'],
										 $_POST['zipCode'], $lat, $lng, $_POST['orgID'], $_POST['comments']);
	
// 	echo $address.'<br>';
// 	echo $geocodeAddress.'<br>';
// 	echo $lat.'<br>';
// 	echo $lng.'<br>';
// 	echo $retval.'<br>'; 
// 	echo TBL_UPDATE_QUEUE.'<br>';
// 	echo $_POST['orgName'].'<br>';
// 	echo  $_POST['websiteAddress'].'<br>';
// 	echo  $_POST['category'].'<br>';
// 	echo  $_POST['orgEmail'].'<br>';
// 	echo  $_POST['orgPhoneNumber'].'<br>';
// 	echo  $_POST['orgFaxNumber'].'<br>';
// 	echo  $_POST['streetAddress'].'<br>';
// 	echo  $_POST['streetAddress2'].'<br>';
// 	echo  $_POST['city'].'<br>';
// 	echo  $_POST['state'].'<br>';
// 	echo  $_POST['zipCode'].'<br>';
// 	echo  $lat.'<br>';
// 	echo  $lng.'<br>';
// 	echo  "comments:".$_POST['comments'].'<br>';
// 	foreach ($form->getErrorArray() as $value)
// 		echo  $value.'<br>';
	
	
	
	
	
	/* Registration Successful */
	if($retval == 0){
		echo "Organization ".$_POST['orgName']." Updates Queued for Apporoval ";
	}
	/* Error found with form */
	else if($retval == 1){
		$_SESSION['value_array'] = $_POST;
		$_SESSION['error_array'] = $form->getErrorArray();
		header("edit_info.php?id=".$_POST['orgID']);
	}
	/* Registration attempt failed */
	else if($retval == 2){
		echo "Organization ".$_POST['orgName']." Failed to update";
	}

}

include("footer.php");

?>